package com.atguigu.orderservice.controller;


import com.atguigu.commonutils.JwtUtils;
import com.atguigu.commonutils.R;
import com.atguigu.orderservice.entity.Order;
import com.atguigu.orderservice.service.OrderService;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;

/**
 * <p>
 * 订单 前端控制器
 * </p>
 *
 * @author lzc
 * @since 2021-01-13
 */
@RestController
@RequestMapping("/orderservice/order")
public class OrderController {

    @Autowired
    private OrderService orderService;

    /**
     * 生成订单
     * @return
     */
    @PostMapping("/saveOrder/{courseId}")
    public R saveOrder(@PathVariable("courseId")String courseId, HttpServletRequest request){
        String orderNo=orderService.saveOrder(courseId, JwtUtils.getMemberIdByJwtToken(request));

        return R.ok().data("orderNo",orderNo);
    }

    //2 根据订单id查询订单信息
    @GetMapping("getOrderInfo/{orderId}")
    public R getOrderInfo(@PathVariable String orderId) {
        QueryWrapper<Order> wrapper = new QueryWrapper<>();
        wrapper.eq("order_no",orderId);
        Order order = orderService.getOne(wrapper);
        return R.ok().data("item",order);
    }


    /**
     * 根据课程id 用户id 支付状态查询是否已经购买
     */

    @GetMapping("isBuy/{courseId}/{memberId}")
    public Boolean isBuy(@PathVariable String courseId,@PathVariable String memberId){
        LambdaQueryWrapper<Order> orderLambdaQueryWrapper = new LambdaQueryWrapper<>();
        orderLambdaQueryWrapper.eq(Order::getCourseId,courseId).eq(Order::getMemberId,memberId).eq(Order::getStatus,1);
        int count = orderService.count(orderLambdaQueryWrapper);
        return count>0;
    }
}

